<template>
    <div class="subpage farm-live-container">
        <div class="farm-live-box">
            <van-list
                v-model="loading"
                :immediate-check="false"
                :finished="finished"
                finished-text="已加载全部了哦～"
                @load="onLoadMore"
            >
                <ul class="farm-live-list">
                    <li v-for="(item, index) in listData" :key="index">
                        <div class="live-ranch-bg" :style="{'background-image': 'url(' + item.farmLivePic + ')'}">
                            <div class="live-ranch-mask">
                                <img src="@/assets/img/live_play_icon.png" alt="" v-if="item.liveType == 2" @click="goFarmLive(item.liveUrl, item.liveType)">
                                <img src="@/assets/img/live_play_icon.png" alt="" v-else @click="goFarmLive(item.liveUrl2, item.liveType)">
                            </div>
                        </div>
                        <p class="live-ranch-name">{{item.farmName}}直播</p>
                    </li>
                </ul>
            </van-list>
        </div>
    </div>
</template>

<script>
import { List } from 'vant';
export default {
    components: {
        [List.name]:List
    },
    data() {
        return {
            listData: [],
            pageIndex: 1,
            pageSize: 10,
            showNoInfo: false,
            loading: false,
            finished: false,
        }
    },
    created() {
        this.getListData()
    },
    methods: {
        getListData() { //获取数据
            let getData = {
                pgCt: this.pageIndex,
                pgSz: this.pageSize
            }
            let url = this.pageIndex > 1 ? '/api/farm/live/list?noLoading' : '/api/farm/live/list'
            this.$api.get(url, getData)
                .then((res) => {
                    this.showNoInfo = res.data.total > 0 ? false : true;
                    this.listData = this.listData.concat(res.data.records)
                    if(this.pageIndex > 1 && res.data.records.length < this.pageSize){
                        this.finished = true;
                    }
                })
        },
        onLoadMore() {
            // 异步更新数据
            setTimeout(() => {
                this.pageIndex++;
                this.getListData()
                // 加载状态结束
                this.loading = false;
            }, 500);
        },
        goFarmLive(liveUrl, liveType) { //去牧场直播页
            this.$router.push({
                name: 'farmLiveDetail',
                query: {
                    farmLive: liveUrl,
                    liveType: liveType
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
    .farm-live-container {
        .farm-live-box {
            width: 100%;
            padding: 0 16px 16px;
            box-sizing: border-box;
            .farm-live-list {
                li {
                    margin-top: 16px;
                    .live-ranch-bg {
                        width: 100%;
                        height: 183px;
                        background-repeat: no-repeat;
                        background-position: center;
                        background-size: cover;
                        .live-ranch-mask {
                            width: 100%;
                            height: 100%;
                            display: flex;
                            flex-direction: column;
                            justify-content: center;
                            align-items: center;
                            background-color: rgba(0, 0, 0, .4);
                            img {
                                width: 66px;
                                height: 66px;
                                object-fit: cover;
                            }
                        }
                    }
                    .live-ranch-name {
                        width: 100%;
                        padding: 12px 16px;
                        box-sizing: border-box;
                        background-color: #fff;
                    }
                }
            }
        }
    }
</style>